/**********************************************************
@description:binary tree, including preorder,inorder,postorder
*File Name:binary_tree.c
*Author: ydx
*mail: yudongxiang93@163.com
*Create Time: Fri 25 Apr 2025 03:41:57 PM CST
**********************************************************/
#include<stdio.h>
#include<stdlib.h>
typedef struct tnode{
	char data;
	struct tnode* lchild;
	struct tnode* rchild;
}tnode;
tnode* init_tnode(int data){
	tnode* n=(tnode*)malloc(sizeof(tnode));
	if(!n){
		printf("init_tnode malloc error!");
		return NULL;
	}
	n->data=data;
	n->lchild=NULL;
	n->lchild=NULL;
	return n;
}
void createTree(tnode** n){
	char data;
	scanf("%c\n",&data);
	if(data == '#'){
		*n=NULL;		
	}else{
		*n=(tnode*)malloc(sizeof(tnode));
		(*n)->data=data;
		createTree(&(*n)->lchild);
		createTree(&(*n)->rchild);
	}
}
void createTree1(tnode **n,char*data,int* idx){
	char ch;
	ch=data[*idx];
	*idx+=1;
	if(ch == '#'){
		*n=NULL;		
	}else{
		*n=(tnode*)malloc(sizeof(tnode));
		(*n)->data=ch;
		createTree1(&(*n)->lchild,data,idx);
		createTree1(&(*n)->rchild,data,idx);
	}

}

typedef void (*visit)(tnode*);
void pn(tnode* n){
	if(n)
		printf("%c ",n->data);
}
void preorder(tnode* t,visit v){
	v(t);
	if(t){
		preorder(t->lchild,v);
		preorder(t->rchild,v);
	}
}
void inorder(tnode* t,visit v){
	if(t){
		inorder(t->lchild,v);
		v(t);
		inorder(t->rchild,v);
	}
}
void postorder(tnode* t,visit v){
	if(t){
		postorder(t->lchild,v);
		postorder(t->rchild,v);
		v(t);
	}
}
int main(int argc,char **argv){
	tnode* t;
	//createTree(&t);
	int index=0;
	createTree1(&t,argv[1],&index);
	preorder(t,pn);
	printf("\n");
	inorder(t,pn);
	printf("\n");
	postorder(t,pn);
	printf("\n");
	return 0;
}
